Xbasic
thread.GetCPUUsageInMilliseconds Method
Syntax
dim Usage as N = thread.GetCPUUsageInMilliseconds()
Description
Returns the total CPU usage (kernel and user times) for the thread in milliseconds with fractional values to the right of the decimal point.
Discussion
The value returned is useful as a reference point for computing total CPU usage between operations. Capture the current value before the operation and then again after the operation. The CPU usage during the operation is the difference between the two values. The example below shows a script that performs a long running operation with calls to the Sleep() function every ten thousand operations thus adding a 100 millisecond delay for every 1000 loops. As a result, the wall clock time and the CPU usage can be differentiated.
Example
t1 = now() tStop = *add_seconds(t1, 5) dim Count as N CPUStart = thread.current().GetCPUUsageInMilliseconds() while now() < tStop s = replicate("abcdefghijklmnop", 100) Count = Count + 1 if mod(count, 10000) = 0 then Sleep(0.100) end if end while CPUEnd = thread.current().GetCPUUsageInMilliseconds() t2 = now() Message = "CPU consumed in seconds: " + ((CPUEnd - CPUStart)/1000) + crlf() \ + "Wall clock time: " + (t2 - t1) + crlf() \ + "Iterations: " + Count showvar(Message)